<?php

namespace app\api\controller\mycurrency;

use app\common\controller\Api;
use app\common\model\mycurrency\DataAlone;
use app\common\model\mycurrency\DataArticle;
use app\common\model\mycurrency\DataArticleClassification;

/**
 * 信息接口
 */
class Information extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 文章分类
    */
    public function articleClass(){
        $params = $this->request->param();
        $rule = [
            ['pid', 'require', '所属分类id不能为空'],
        ];
        $check = $this->validate($params, $rule);
        if ($check !== true) {
            $this->error($check);
        }
        $where = [
            'status' => DataArticleClassification::STATUS_XIANSHI,
            'deletetime' => null,
        ];
        if($params['pid'] != null){
            $where['pid'] = $params['pid'];
        }else{
            $where['pid'] = 0;
        }
        $data = DataArticleClassification::where($where)->field('id,name,image')->order('weigh desc')->select();
        $this->success('加载成功',$data);
    }


    /**
     * 文章列表
     */
    public function articleList(){
        $params = $this->request->param();
        $rule = [
            ['page', 'require', '页码不能为空'],
            ['pageSize', 'require', '每页查询条数不能为空'],
            ['article_class_id','require','分类id不能为空']
        ];
        $check = $this->validate($params, $rule);
        if ($check !== true) {
            $this->error($check);
        }

        $where = [
            'article_class_id' => $params['article_class_id'],
            'status' => DataArticle::STATUS_XIANSHI,
            'deletetime' => null,
        ];

        $list = DataArticle::where($where)
            ->limit(($params['page'] - 1) * $params['pageSize'], $params['pageSize'])
            ->order('weigh desc,id desc')
            ->field('id,title,image,abstract,createtime')
            ->select();
        $num = DataArticle::where($where)->count();
        if ($num > $params['page'] * $params['pageSize']) {
            $nextpage = true;
        } else {
            $nextpage = false;
        }
        $this->success('获取成功', ['list' => $list, 'nextpage' => $nextpage]);
    }

    /**
     * 文章详情
     */
    public function articleInfo(){
        $params = $this->request->param();
        $rule = [
            ['article_id','require','文章id不能为空']
        ];
        $check = $this->validate($params, $rule);
        if ($check !== true) {
            $this->error($check);
        }

        $where = [
            'id' => $params['article_id'],
            'status' => DataArticle::STATUS_XIANSHI,
            'deletetime' => null,
        ];

        $data = DataArticle::where($where)->find();
        if(!$data){
            $this->error('文章不存在');
        }
        $this->success('获取成功',$data);
    }

    /**
     * 系统单页
     */
    public function alone(){
        $param = $this->request->param();
        $rule = [
            ['type', 'require|in:women,shiyong,yinsi,zulin,mianya,return', '单页类型不能为空|类型错误'],
        ];
        $check = $this->validate($param, $rule);
        if ($check !== true) {
            $this->error($check);
        }
        $where = [];
        switch ($param['type']){
            case 'women'://关于我们
                $where['id'] = 1;
                break;
            case 'shiyong'://使用协议
                $where['id'] = 2;
                break;
            case 'yinsi'://隐私协议
                $where['id'] = 3;
                break;
            case 'zulin'://租赁协议
                $where['id'] = 4;
                break;
            case 'mianya'://租赁协议
                $where['id'] = 5;
                break;
            case 'return'://归还流程说明
                $where['id'] = 6;
                break;
            default:
                $this->error('类型参数错误');
                break;
        }
        $info = DataAlone::where($where)->find();
        $this->success('加载成功',$info);
    }
}
